<template>
  <div class="container">
    <div class="tags-container">
      <el-card>
        <div slot="header" v-if="Object.keys(subject).length !== 0" ref="showheader">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-row>
          <el-col :span="18">
            <el-form ref="tagsForm" :model="tagsInfo" style="display: flex">
              <el-form-item label="标签名称" prop="tagName" label-width="80px">
                <el-input v-model="tagsInfo.tagName" />
              </el-form-item>
              <el-form-item label="状态：" prop="state" label-width="80px">
                <el-select v-model="tagsInfo.state">
                  <el-option
                    v-for="item in status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" style="margin-left: 10px" @click="resetForm"
                  >清除</el-button
                >
                <el-button
                  size="small"
                  type="primary"
                  style="margin-left: 10px"
                  @click="handleFilter"
                  >搜索</el-button
                >
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-row>
              <el-col :span="24">
                <el-button type="text" size="small" @click="$router.push('list')" v-if="Object.keys(subject).length !== 0">
                  <i class="el-icon-back"></i>
                  <span>返回学科</span>
                </el-button>
                <el-button type="success" size="small" @click="AddTagsFn('add')">
                  <i class="el-icon-edit"></i>
                  <span>新增标签</span>
                </el-button>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 警告信息 -->
        <el-alert
          :title="tagstitle"
          type="info"
          show-icon
          :closable="false"
          style="margin-bottom: 15px"
        />
        <el-table
          :data="dataList"
          v-loading="listLoading"
          :header-cell-style="{ 'text-align': 'center' }"
          :cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column type="index" :index="indexMethod" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" min-width="80"> </el-table-column>
          <el-table-column prop="tagName" label="标签名称" min-width="80"> </el-table-column>
          <el-table-column prop="username" label="创建者" min-width="80"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期" min-width="80">
            <template #default="{ row }">
              <!-- {{row}} -->
              {{ formatTimeFn(row.addDate) }}
            </template>
          </el-table-column>
          <!-- <el-table-column prop="totals" label="面试题数量" min-width="80"> </el-table-column> -->
          <el-table-column prop="state" label="状态" min-width="80">
            <template #default="{ row }">
              {{ row.state ? '已开启' : '已屏蔽' }}
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" width="150">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="EnableFn(row)">{{
                row.state ? '屏蔽' : '开启'
              }}</el-button>
              <el-button
                type="text"
                size="small"
                :disabled="Boolean(row.state)"
                @click="AddTagsFn(row)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                :disabled="Boolean(row.state)"
                @click="delTags(row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px; display: flex; justify-content: flex-end">
          <el-pagination
            small
            background
            layout="sizes, prev, pager, next, jumper"
            :total="counts"
            :current-page="requestParameters.page"
            :page-size="requestParameters.pagesize"
            :page-sizes="[5, 10, 15, 20]"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
      <tags-add
        ref="tags"
        :formData.sync="formData"
        :dialogVisible.sync="dialogVisible"
        :dialogTitle="dialogTitle"
      />
    </div>
  </div>
</template>

<script>
import { list, remove, detail, changeState } from '@/api/hmmm/tags'
import { status } from '@/api/hmmm/constants'
import dayjs from 'dayjs'
import TagsAdd from '@/module-hmmm/components/tags-add.vue'
export default {
  name: '',
  components: { TagsAdd },
  data () {
    return {
      /** 搜索框 */
      tagsInfo: {
        tagName: '',
        subjectID: ''
      },
      tagstitle: '数据一共xx条', // 轻警告提示
      requestParameters: {
        page: 1, // 当前页码
        pagesize: 10 // 单页条数
      },
      counts: 0, // 总条数
      dataList: [], // table 展示数据
      dialogVisible: false,
      dialogTitle: '新增学科',
      listLoading: false,
      disabled: false,
      showheader: false,
      formData: {}
    }
  },
  computed: {
    status () {
      return status
    },
    subject () {
      return this.$route.query || {}
    }
  },
  watch: {
    '$route.query': function (to, form) {
      this.tagsListApi({ subjectID: this.subject.id })
    }
  },
  created () {
    this.tagsListApi()
  },
  methods: {
    /** 获取列表数据，进行页面展示 */
    async tagsListApi (data) {
      this.listLoading = true
      const {
        data: { counts, items }
      } = await list({ ...this.requestParameters, ...data })
      this.counts = counts
      this.dataList = items
      this.tagstitle = '数据一共' + counts + '条'
      this.listLoading = false
    },
    /** 是否开启 */
    EnableFn (data) {
      let str = ''
      if (data.state === 1) {
        str = '屏蔽'
        data.state = 0
      } else {
        str = '开启'
        data.state = 1
      }
      const obj = {
        id: data.id,
        state: data.state
      }
      this.$confirm('你确定要' + str + '吗', '提示')
        .then(async () => {
          await changeState(obj)
          this.$message.success(str + '成功')
        })
        .catch(() => {
          if (data.state === 0) {
            data.state = 1
          } else {
            data.state = 0
          }
          this.$message.success(str + '已取消')
        })
    },
    // 重置
    resetForm () {
      this.$refs.tagsForm.resetFields()
    },
    // 搜索信息
    handleFilter () {
      this.requestParameters.page = 1
      const { tagName, state } = this.tagsInfo
      if (tagName.length && (state || !state)) {
        this.tagsListApi({ tagName, state })
      }
      if (tagName.length) this.tagsListApi({ tagName })
      state ? this.tagsListApi({ state }) : this.tagsListApi({ state })
    },
    /** 自定义序号 */
    indexMethod (index) {
      return (this.requestParameters.page - 1) * this.requestParameters.pagesize + index + 1
    },
    /** 入职时间格式处理 */
    formatTimeFn (data) {
      return dayjs(data).format('YYYY-MM-DD hh:mm:ss')
    },
    /** 分页---每页条目发送变化时 */
    handleSizeChange (val) {
      // console.log(val)
      this.requestParameters.pagesize = val
      this.requestParameters.page = 1
      this.tagsListApi(this.requestParameters)
    },
    /** 分页---页码发生变化时 */
    handleCurrentChange (val) {
      // console.log(val)
      this.requestParameters.page = val
      this.tagsListApi(this.requestParameters)
    },
    /** 打开添加学科 dialog */
    AddTagsFn (data) {
      this.dialogVisible = true
      if (data === 'add') {
        this.dialogTitle = '新增标签'
      } else {
        this.dialogTitle = '编辑标签'
        this.editSubjects(data)
      }
    },
    /** 删除标签 */
    delTags (data) {
      this.$confirm('你确定要删除该标签吗？', '提示')
        .then(async () => {
          await remove(data)
          this.$message.success('标签删除成功')
          this.tagsListApi()
        })
        .catch(() => {})
    },

    /** 获取标签详情 */
    async editSubjects (data) {
      const { data: res } = await detail({ id: data.id })
      this.formData = res
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  .tags-container {
    // padding: 10px;
    .el-card {
      .el-row:after {
        clear: both;
      }
      .el-row:before {
        display: table;
      }
      .el-form-item--medium {
        margin-bottom: 18px;
      }
    }
  }
}
</style>
